package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.exception.AppException;
import com.eling.elcms.core.model.BaseModel;
import com.eling.elcms.core.service.IGenericModelService;
import com.eling.elcms.core.service.ServiceLocator;
import com.eling.elcms.system.model.ElcmsUser;
import com.eling.elcms.travel.dao.IApprovalProcessDao;
import com.eling.elcms.travel.model.ApprovalProcess;
import com.eling.elcms.travel.model.SendMsgTpye;
import com.eling.elcms.travel.model.view.PendingApprovalView;
import com.eling.elcms.travel.service.ICallbackService;
import com.eling.elcms.travel.service.IPendingApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
@Service
public class PendingApprovalServiceImpl implements IPendingApprovalService {

	@Autowired
	private IApprovalProcessDao approvalProcessDao;
	@Autowired
	private IGenericModelService genericModelService;

	@Override
	public List<PendingApprovalView> queryPendingApproval() {
		
		List<PendingApprovalView> resultList = new ArrayList<>();
		
		//查询该用户对应的审批流程
		ApprovalProcess appProcess = new ApprovalProcess();
		appProcess.setChecker((ElcmsUser) AppContext.curUser());
		appProcess.setStatus(ApprovalProcess.Status.Initial);
		List<ApprovalProcess> appProcessList = approvalProcessDao.query(appProcess);
		
		for(ApprovalProcess process : appProcessList){
			//获取对应审批流程的审批明细
			Set<ApprovalProcess> itemProcess=process.getApprovalInstance().getApprovalProcesses();
			ApprovalProcess commitProcess=new ApprovalProcess();
			List<ApprovalProcess> beforeProcessList=new ArrayList<>();
			List<ApprovalProcess> beforeCommitProcessList=new ArrayList<>();
			Iterator<ApprovalProcess> it=itemProcess.iterator();
			while(it.hasNext()){
				ApprovalProcess beforeprocess=it.next();
				//之前的审批人
				if(beforeprocess.getSequenceNumber()<process.getSequenceNumber()){
					beforeProcessList.add(beforeprocess);
					if(beforeprocess.getCommitTime()!=null&&beforeprocess.getStatus().equals(ApprovalProcess.Status.Commit)){//提交人
						commitProcess=beforeprocess;
					}else if(beforeprocess.getCheckTime()!=null&&!beforeprocess.getStatus().equals(ApprovalProcess.Status.Initial)){
						beforeCommitProcessList.add(beforeprocess);
					}
				}else if(beforeprocess.getSequenceNumber().equals(process.getSequenceNumber())){
					if(!beforeprocess.getChecker().equals(process.getChecker())){
						if(!beforeprocess.getStatus().equals(ApprovalProcess.Status.Initial)){
							beforeProcessList.add(beforeprocess);
							beforeCommitProcessList.add(beforeprocess);
						}
					}
				}
			}
			if(beforeProcessList.size()==(beforeCommitProcessList.size()+1)){
				//取最新的审批
				ApprovalProcess lastOne=new ApprovalProcess();
				Date beforTime=new Date();
				if(beforeProcessList.size()==1){//排第一位
					lastOne=commitProcess;
					beforTime=lastOne.getCommitTime();
				}else{
					Collections.sort(beforeCommitProcessList,new Comparator<ApprovalProcess>(){
						@Override
						public int compare(ApprovalProcess o1,ApprovalProcess o2) {
							return o2.getCheckTime().compareTo(o1.getCheckTime());
						}});
					lastOne=beforeCommitProcessList.get(0);
					beforTime=lastOne.getCheckTime();
				}
				 
				 
				PendingApprovalView result = new PendingApprovalView();
				BaseModel model = null;
				ICallbackService callbackService = ServiceLocator.getService(process.getApprovalInstance().getApprovalDefine().getServiceImpl());
				Long modelId = process.getApprovalInstance().getModelId();
				String modelClass = process.getApprovalInstance().getApprovalDefine().getModelClass();
				try {
					model = (BaseModel) genericModelService.getById(modelId,
							Class.forName(modelClass));
				} catch (Exception e) {
					throw  new AppException("类型错误");
				}
				String title = callbackService.getMsgTitle(model, SendMsgTpye.Procsss);
				
				result.setPkApprovalProcess(process.getPkApprovalProcess());
				result.setCommitTime(commitProcess.getCommitTime());
				result.setBeforeProcess(lastOne);
				result.setBeforeTime(beforTime);
				result.setProcesses(process);
				result.setNo(process.getSequenceNumber());
				result.setModel(model);
				result.setMsg(title);
				result.setApprovalInstance(process.getApprovalInstance());
				result.setServiceType(process.getApprovalInstance().getApprovalDefine().getServiceType());
				result.setDescription(process.getApprovalInstance().getApprovalDefine().getDescription());
				resultList.add(result);
			}
		}
		 Collections.sort(resultList,new Comparator<PendingApprovalView>(){
				@Override
				public int compare(PendingApprovalView o1,PendingApprovalView o2) {
					return o1.getCommitTime().compareTo(o2.getCommitTime());
				}});
		return resultList;
	}
}
